﻿using System;
using System.Collections.Generic;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.select
{
	// Token: 0x02000230 RID: 560
	public class SelectKeyWordAlignProcessor : AbstractKeyWordAlignProcessor
	{
		// Token: 0x060012CA RID: 4810 RVA: 0x0031A608 File Offset: 0x00318808
		protected internal override IList<TSourceToken[]> getTSourceToken<E>(E arg)
		{
			TSelectSqlStatement tselectSqlStatement = arg as TSelectSqlStatement;
			IList<TSourceToken[]> list = new List<TSourceToken[]>();
			TSourceToken selectToken = tselectSqlStatement.SelectToken;
			if (selectToken != null)
			{
				list.Add(new TSourceToken[]
				{
					selectToken
				});
			}
			if (tselectSqlStatement.IntoClause != null)
			{
				TSourceToken startToken = tselectSqlStatement.IntoClause.startToken;
				list.Add(new TSourceToken[]
				{
					startToken
				});
			}
			if (tselectSqlStatement.joins.size() > 0)
			{
				TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(tselectSqlStatement.joins.startToken, 5, "from");
				if (tsourceToken == null && tselectSqlStatement.tables != null)
				{
					tsourceToken = SourceTokenSearcher.backforwardSearch(tselectSqlStatement.joins.startToken, 5, "from");
				}
				if (tsourceToken != null)
				{
					list.Add(new TSourceToken[]
					{
						tsourceToken
					});
				}
			}
			if (tselectSqlStatement.GroupByClause != null)
			{
				TSourceToken group = tselectSqlStatement.GroupByClause.GROUP;
				TSourceToken by = tselectSqlStatement.GroupByClause.BY;
				if (group != null && by != null)
				{
					list.Add(new TSourceToken[]
					{
						group,
						by
					});
				}
				TSourceToken having = tselectSqlStatement.GroupByClause.HAVING;
				if (having != null)
				{
					list.Add(new TSourceToken[]
					{
						having
					});
				}
			}
			if (tselectSqlStatement.OrderbyClause != null)
			{
				TSourceToken startToken2 = tselectSqlStatement.OrderbyClause.startToken;
				TSourceToken tsourceToken2 = SourceTokenSearcher.forwardSearch(tselectSqlStatement.OrderbyClause.startToken, 5, "by");
				list.Add(new TSourceToken[]
				{
					startToken2,
					tsourceToken2
				});
			}
			if (tselectSqlStatement.WhereClause != null)
			{
				TSourceToken startToken3 = tselectSqlStatement.WhereClause.startToken;
				list.Add(new TSourceToken[]
				{
					startToken3
				});
			}
			if (tselectSqlStatement.joins != null && this.Option.alignJoinWithFromKeyword)
			{
				for (int i = 0; i < tselectSqlStatement.joins.size(); i++)
				{
					TJoin join = tselectSqlStatement.joins.getJoin(i);
					for (int j = 0; j < join.JoinItems.size(); j++)
					{
						TJoinItem joinItem = join.JoinItems.getJoinItem(j);
						if (joinItem != null)
						{
							TSourceToken beginJoinToken = JoinOnProcessor.getBeginJoinToken(joinItem);
							if (beginJoinToken.astext.TrimEnd(new char[1]).Trim().Equals("join", StringComparison.OrdinalIgnoreCase))
							{
								list.Add(new TSourceToken[]
								{
									beginJoinToken
								});
							}
							else
							{
								TSourceToken tsourceToken3 = SourceTokenSearcher.forwardSearch(beginJoinToken, 5, "join");
								list.Add(new TSourceToken[]
								{
									beginJoinToken,
									tsourceToken3
								});
							}
						}
					}
				}
			}
			return list;
		}
	}
}
